package manager
{
	import event.EventManager;
	import event.GameEvent;
	
	import flash.utils.Dictionary;
	
	import valueObject.KingVO;

	/**
	 *皇上管理 
	 * @author SXT
	 * 支持多皇上的失败条件
	 */
	public class KingManager extends BaseManager implements IFram
	{
		/**
		 *皇上 
		 */
		public var king:KingVO;
		/**
		 *当前关卡配置 
		 */
		private var levelVO:LevelVO;
		/**
		 *与本类有关的配置文件 
		 */		
		private var config:Dictionary;
		
		public function KingManager()
		{
			levelVO = Game.instance.main.levelManager.levelvo;
			king = new KingVO();
		}
		
		public function updataConfig(level:int):void{
			config =  Game.instance.main.conl["configDic"]["king"];
			king = getConfig(levelVO.king,king);
			levelBegin();
		}
		
		public function framUpdata(index:int):void
		{
			
		}
		/**
		 *从配置文件里读取一个皇上的配置 
		 * @param id	皇上的id
		 * @param king	
		 * @return 
		 */		
		private function getConfig(id:int, king:KingVO):KingVO
		{
			for (var tmp:String in  config[id]){
				try{
					king[tmp] =config[id][tmp];
				}catch(e:Error){
					try{
						if(String(config[id][tmp]).indexOf("#") != -1){
							king[tmp] = String(config[id][tmp]).split("#");
						}else{
							king[tmp].push(config[id][tmp]);
						}
					}catch(e:Error){
					}finally{
						trace(king[tmp]);
					}
	//						trace(e.message);
	//						trace("monster"+"的"+tmp+"属性不存在，请核对配置文件或变量命名" );
				}
			}
			return king;
		}
		
		/**
		 *加血 
		 * @param value
		 */
		public function addHP(value:int):void{
			king.cutHP += value;
		}
		/**
		 *减血 
		 * @param value
		 */		
		public function cutHP(value:int):void{
			king.cutHP -= (value - king.defense);
			if(king.cutHP <= 0){
				EventManager.instance.dispatchEvent(new GameEvent(GameEvent.GAME_STATE_END));
			}
			trace(king.cutHP);
		}
		
		/**
		 *设置皇帝的位置 
		 */
		private function levelBegin():void{
			king.x = king.local[0];
			king.y = king.local[1];
			Game.instance.mtLayer.addChild(king);
		}
	}
}